package com.example.rentingapp.activity;

import android.animation.Animator;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.example.rentingapp.R;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.LoginResponse;
import com.example.rentingapp.util.StringUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private Button btnLogin;
    private LottieAnimationView animationView;
    private EditText etAccount, etPassword;
    private CheckBox cbRemember;
    private TextView tvCreatAnAccount;
    private String account, password;

    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        btnLogin = findViewById(R.id.btn_go);
        cbRemember = findViewById(R.id.cb_remember);
        etAccount = findViewById(R.id.et_login_account);
        etPassword = findViewById(R.id.et_login_password);
        tvCreatAnAccount = findViewById(R.id.tv_create_an_account);
        animationView = findViewById(R.id.lottie_login);
    }

    @Override
    protected void initData() {
        btnLogin.setOnClickListener(this);
        tvCreatAnAccount.setOnClickListener(this);

        if (getBooleanByKey("checkboxBoolean", false)) {
            cbRemember.setChecked(true);
            etAccount.setText(findByKey("account"));
            etPassword.setText(findByKey("password"));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_go:
                account = etAccount.getText().toString().trim();
                password = etPassword.getText().toString().trim();
                //按钮被选中，下次进入时会显示账号和密码
                //按钮被选中，清空账号和密码，下次进入时会显示账号和密码为空
                if (cbRemember.isChecked()) {
                    insertVal("account", account);//将账号保存到本地
                    insertVal("password", password);//将密码保存到本地
                    putBooleanByKey("checkboxBoolean", true);
                } else {
                    removeByKey("account");
                    removeByKey("password");
                    putBooleanByKey("checkboxBoolean", false);
                }
                removeByKey("user_id_register");
                login(account, password);
                break;
            case R.id.tv_create_an_account:
                navigateTo(RegisterActivity.class);
                finish();
                break;
        }
    }

    private void login(String account, String password) {
        if (StringUtils.isEmpty(account)) {
            showToast("请输入账号");
            return;
        }
        if (StringUtils.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("account", account);
        params.put("password", password);
        Api.config(ApiConfig.LOGIN, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(final String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                LoginResponse loginResponse = gson.fromJson(res, LoginResponse.class);

                String user_id = String.valueOf(loginResponse.getDatas().get(0).getUserId());
                insertVal("user_id", user_id);//将user_id保存到本地

                if (loginResponse.getSuccess() == true) {
                    insertVal("logged", "已登录");
                    navigateToWithFlag(HomeActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    showToastSync("登录成功");
                } else {
                    showToastSync("用户名或密码错误");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}
